package edu.usc.epigenome.dnaase.script;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

public class vcfConcordance {

	final private static String USAGE = "vcfConcordance [opts] vcfFileName1 vcfFileName2 ...";
	/**
	 * @param args
	 */
	@Option(name="-samtools",usage="samtools vcf format, then should put samtools vcf file in the second input file")
	protected boolean samtools = false;
	@Option(name="-verbose",usage="output additional infor")
	protected boolean verbose = false;
	
	@Argument
	private List<String> stringArgs = new ArrayList<String>();
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		new vcfConcordance().doMain(args);
	}

	public void doMain(String[] args) throws IOException{
		CmdLineParser parser = new CmdLineParser(this);
		// if you have a wider console, you could increase the value;
		// here 80 is also the default
		parser.setUsageWidth(80);
		try
		{
			parser.parseArgument(args);
			if (stringArgs.size() < 2) throw new CmdLineException(USAGE);
		
		}
		catch (CmdLineException e)
		{
			System.err.println(e.getMessage());
			// print the list of available options
			parser.printUsage(System.err);
			System.err.println();
			return;
		}

		String vcfFileName1 = stringArgs.remove(0);
		String vcfFileName2 = stringArgs.remove(0);
		HashMap<String,Double> vcf1map = new HashMap<String,Double>();
		BufferedReader br1 = new BufferedReader(new FileReader(vcfFileName1));
		int hetConcordance = 0;
		int het1_hom2 = 0;
		int hom1_het2 = 0;
		int het1 = 0;
		int het2 = 0;
		int totalCount1 = 0;
		int totalCount2 = 0;
		
		//int hetConcordanceKnownSnp = 0;
		//int het1_hom2KnownSnp = 0;
		//int hom1_het2KnownSnp = 0;
		//int het1KnownSnp = 0;
		//int het2KnownSnp = 0;
		//int totalCount1KnownSnp = 0;
		//int totalCount2KnownSnp = 0;
		
		String line;
		while( (line = br1.readLine()) != null){
			if(line.startsWith("#"))
				continue;
			
			String[] tmpArray = line.split("\t");
			//if(!tmpArray[6].startsWith("PASS"))
				//continue;
			boolean knownSNP = false;
			if(tmpArray[2].startsWith("rs"))
				knownSNP = true;
			String key = tmpArray[0] + "-" + tmpArray[1];
			String[] attributes = tmpArray[7].split(";");
			double word = -1;
			for(String attribute : attributes){
				
					if(attribute.matches("AF=\\S+")){
						String[] temp = attribute.split("=");
						String[] tempAF = temp[1].split(",");
						word = Double.parseDouble(tempAF[0]);
					}
				
			}
			if(word != -1){
				
				if(word != 0 && word != 1.0){
					het1++;
					vcf1map.put(key, word);
					//if(knownSNP)
						//het1KnownSnp++;
				}
					
				
			}
			totalCount1++;
			//if(knownSNP)
				//totalCount1KnownSnp++;
		}
		br1.close();
		BufferedReader br2 = new BufferedReader(new FileReader(vcfFileName2));
		RECORD: while( (line = br2.readLine()) != null){
			if(line.startsWith("#"))
				continue;
			String[] tmpArray = line.split("\t");
			//if(!tmpArray[6].startsWith("PASS") && !samtools)
			//	continue;
			boolean knownSNP = false;
			if(tmpArray[2].startsWith("rs"))
				knownSNP = true;
			String key = tmpArray[0] + "-" + tmpArray[1];
			String[] attributes = tmpArray[7].split(";");
			double word = -1;
			for(String attribute : attributes){
				if(samtools){
					if(attribute.startsWith("INDEL")){
						continue RECORD;
					}
					if(attribute.matches("AF1=\\S+")){
						String[] temp = attribute.split("=");
						String[] tempAF = temp[1].split(",");
						word = Double.parseDouble(tempAF[0]);
					}
				}
				else{
					if(attribute.matches("AF=\\S+")){
						String[] temp = attribute.split("=");
						String[] tempAF = temp[1].split(",");
						word = Double.parseDouble(tempAF[0]);
					}
				}
			}
			if(vcf1map.containsKey(key)){
				
				double value = vcf1map.get(key);
				if((value != 0 && value != 1.0) && (word != 0 && word != 1.0 && word != -1)){
					if(samtools){
						if(tmpArray[9].startsWith("0/1") || tmpArray[9].startsWith("1/0")){
							hetConcordance++;
						}
						else{
							if(verbose){
								System.out.println("1: " + key);
							}
						}
					}
					else{
						hetConcordance++;
					}
					
					//if(knownSNP)
						//hetConcordanceKnownSnp++;
				}
				else{
					if(verbose){
						System.out.println("2: " + key);
					}
				}
				
			}
			else{
				if(word != 0 && word != 1.0 && word != -1){
					if(samtools){
						if(tmpArray[9].startsWith("0/1") || tmpArray[9].startsWith("1/0")){
							if(verbose){
								System.out.println("3: " + key);
							}
						}
					}
					else{
						if(verbose){
							System.out.println("4: " + key);
						}
					}
					
				}
					
				
			}
			if(word != 0 && word != 1.0 && word != -1){
				if(samtools){
					if(tmpArray[9].startsWith("0/1") || tmpArray[9].startsWith("1/0")){
						het2++;
					}
				}
				else{
					het2++;
				}
				
				//if(knownSNP)
				//	het2KnownSnp++;
			}
				
			totalCount2++;
			//if(knownSNP)
			//	totalCount2KnownSnp++;
		}
		br2.close();
		System.out.println("totalCount1: " + totalCount1);
		System.out.println("het1: " + het1);
		System.out.println("totalCount2: " + totalCount2);
		System.out.println("het2: " + het2);
		System.out.println("hetConcordance: " + hetConcordance);
		System.out.println("het1_hom2: " + het1_hom2);
		System.out.println("hom1_het2: " + hom1_het2);
		
		//System.out.println("totalCount1KnownSnp: " + totalCount1KnownSnp);
		//System.out.println("het1KnownSnp: " + het1KnownSnp);
		//System.out.println("totalCount2KnownSnp: " + totalCount2KnownSnp);
		//System.out.println("het2KnownSnp: " + het2KnownSnp);
		//System.out.println("hetConcordanceKnownSnp: " + hetConcordanceKnownSnp);
		//System.out.println("het1_hom2KnownSnp: " + het1_hom2KnownSnp);
		//System.out.println("hom1_het2KnownSnp: " + hom1_het2KnownSnp);
	}
}
